export async function init() {

    document.querySelectorAll('.filter-item').forEach(item => {
        item.addEventListener('click', function () {
            document.querySelectorAll('.filter-item').forEach(i => {
                i.classList.remove('active');
            });
            this.classList.add('active');
            // 这里可以添加筛选逻辑
            console.log('筛选分类:', this.textContent);
        });
    });

    // 分页按钮点击事件
    document.querySelectorAll('.page-btn:not(.disabled)').forEach(btn => {
        btn.addEventListener('click', function () {
            if (this.classList.contains('active')) return;

            document.querySelectorAll('.page-btn').forEach(b => {
                b.classList.remove('active');
            });

            if (!this.querySelector('i')) {
                this.classList.add('active');
            }
            // 这里可以添加分页逻辑
            console.log('切换到页码:', this.textContent);
        });
    });

    // 查看按钮点击事件
    document.querySelectorAll('.view-btn').forEach(btn => {
        btn.addEventListener('click', function () {
            const knowledgeCard = this.closest('.knowledge-card');
            let knowledgeUrl = "pages/knowledge-detail.html";
            if (this.dataset.id === '1') {
                knowledgeUrl = "pages/knowledge-detail.html"
            } else if (this.dataset.id === '2') {
                knowledgeUrl = "pages/knowledge-detail2.html"
            }
            fetch(knowledgeUrl).then(response => response.text()).then(html => {
                const modal = document.querySelector('.modal');
                document.getElementById('main-content').innerHTML = html;
            });
        });
    });

    // 管理按钮点击事件
    document.querySelectorAll('.manage-btn').forEach(btn => {
        btn.addEventListener('click', async function () {
            const knowledgeCard = this.closest('.knowledge-card');
            const id = "knowledge-manage"
            const page = "pages/knowledge-manage.html"
            window.open(page, '_blank');
           /* const index = await import('./index.js');
            index.loadContent(page,id);*/
        });
    });

    const newKnowledgeBtn = document.getElementById('newKnowledgeBtn');
    const addNewCard = document.getElementById('addNewCard');
    const modal = document.getElementById('newKnowledgeModal');
    const modalClose = document.getElementById('modalClose');
    const cancelBtn = document.getElementById('cancelBtn');
    const knowledgeForm = document.getElementById('knowledgeForm');
    const openModal = () => {
        modal.classList.add('active');
        document.body.style.overflow = 'hidden';
    }

    const closeModal = () => {
        modal.classList.remove('active');
        document.body.style.overflow = '';
    }
    // 点击新建知识库按钮
    newKnowledgeBtn.addEventListener('click', openModal);
    // 点击添加新卡片
    //addNewCard.addEventListener('click', openModal);
    // 点击关闭按钮
    modalClose.addEventListener('click', closeModal);
    // 点击取消按钮
    cancelBtn.addEventListener('click', closeModal);
    // 点击模态框外部关闭
    modal.addEventListener('click', async function (e) {
        if (e.target === modal) {
            closeModal()
        }
    });

    // 表单提交
    knowledgeForm.addEventListener('submit', function (e) {
        e.preventDefault();
        // 获取表单数据
        const name = document.getElementById('knowledgeName').value;
        const desc = document.getElementById('knowledgeDesc').value;
        const category = document.getElementById('knowledgeCategory').value;
        const permission = document.getElementById('knowledgePermission').value;
        const icon = document.querySelector('input[name="knowledgeIcon"]:checked').value;
        // 这里可以添加AJAX请求来保存数据
        console.log('创建知识库:', {name, desc, category, permission, icon});
        closeModal()
        // 显示成功消息
        alert('知识库创建成功！');
        // 重置表单
        knowledgeForm.reset();
    });

    // 分类筛选功能
    const filterItems = document.querySelectorAll('.filter-item');
    filterItems.forEach(item => {
        item.addEventListener('click', function () {
            filterItems.forEach(i => i.classList.remove('active'));
            this.classList.add('active');
            // 这里可以添加筛选逻辑
        });
    });
}


